//package com.shuma.approval.task;
//
//import com.shuma.approval.constant.MessageConstant;
//import com.shuma.approval.constant.OnlineStatusConstant;
//import com.shuma.approval.constant.OperatorStatusConstant;
//import com.shuma.approval.constant.RedisConstants;
//import com.shuma.approval.entity.GoodsBaseInfo;
//import com.shuma.approval.entity.OperatorLog;
//import com.shuma.approval.service.IGoodsBaseInfoService;
//import com.shuma.approval.service.IOperatorLogService;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.data.redis.core.StringRedisTemplate;
//import org.springframework.scheduling.annotation.Scheduled;
//import org.springframework.stereotype.Component;
//
//import java.time.Instant;
//import java.time.LocalDateTime;
//import java.time.ZoneOffset;
//import java.util.Collection;
//import java.util.List;
//import java.util.Map;
//import java.util.stream.Collectors;
//
//@Component
//public class ToOnlineTask {
//
//    @Autowired
//    private StringRedisTemplate stringRedisTemplate;
//    @Autowired
//    private IGoodsBaseInfoService goodsBaseInfoService;
//    @Autowired
//    private IOperatorLogService operatorLogService;
//
//    //利用定时任务自动上线
//    // 每五分钟执行一次
//    @Scheduled(cron = "0 0/5 * * * ? ")
//    public void redisToOnline() {
//        Map<Object, Object> map = stringRedisTemplate.opsForHash().entries(RedisConstants.TO_ONLINE);
//        Collection<Object> values = map.values();
//        // 使用 LocalDateTime 和 UTC 时区获取当前时间的毫秒值
//        Instant instant = LocalDateTime.now().toInstant(ZoneOffset.UTC);
//        long now = instant.toEpochMilli();
//        // 过滤出小于当前时间毫秒值的值并找到对应的键
//        List<Object> keysWithValuesLessThanNow = map.entrySet().stream()
//                .filter(entry -> {
//                    Object value = entry.getValue();
//                    if (value instanceof String) {
//                        try {
//                            long timeValue = Long.parseLong((String) value);
//                            return timeValue < now;
//                        } catch (NumberFormatException e) {
//                            throw new IllegalArgumentException("Value is not a valid Long: " + value);
//                        }
//                    } else {
//                        throw new IllegalArgumentException("Value is not of type String: " + value);
//                    }
//                })
//                .map(Map.Entry::getKey)
//                .collect(Collectors.toList());
//
//        // 遍历上线
//        for (Object o : keysWithValuesLessThanNow) {
//            try {
//                // 将 Object 强制转换为 String
//                String str = (String) o;
//                // 将 String 转换为 long
//                long id = Long.parseLong(str);
//                goodsBaseInfoService.update()
//                        .set("goods_online_status", OnlineStatusConstant.IS_ONLINE)
//                        .eq("goods_base_id", id).update();
//                //记录操作日志
//                GoodsBaseInfo goodsBaseInfo = goodsBaseInfoService.getById(id);
//                OperatorLog operatorLog = new OperatorLog();
//                operatorLog.setOwnerId(goodsBaseInfo.getOperatorId());
//                operatorLog.setCommodityId(id);
//                operatorLog.setOperatorStatus(OperatorStatusConstant.GO_ONLINE);
//                operatorLog.setRemark(MessageConstant.AUTO_ONLINE);
//                operatorLog.setOwner(goodsBaseInfo.getOperator());
//                operatorLog.setOperatorTime(LocalDateTime.now());
//                //删除商品操作记录redis缓存
//                String key = RedisConstants.OPERATORLOG + id;
//                stringRedisTemplate.delete(key);
//                //删除商品操作记录redis缓存
//                stringRedisTemplate.opsForHash().delete(RedisConstants.TO_ONLINE,str);
//                operatorLogService.save(operatorLog);
//                // 输出结果
//            } catch (ClassCastException e) {
//                System.out.println("The object is not a String: " + o);
//            } catch (NumberFormatException e) {
//                System.out.println("The string is not a valid long: " + o);
//            }
//
//        }
//    }
//
//
//}
